package com.wwwarehouse.usercenter.fragment.businessunit;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NetUtils;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.ImageloaderUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.BusinessunitInfo;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import java.util.HashMap;
import java.util.Map;

/**
 * Creator by zhenwang.xiang on 2017/9/22 17:02
 * Description: Gridview点击item进入展示顶级业务单位卡片
 */

public class BusinessUnitInfoFragment extends BaseFragment {
    private ImageView mImgLogo;
    private TextView mTvName;
    private TextView mTvPhone;
    private TextView mTvAddress;
    private TextView mTvDetialaddress;
    private ImageView mImgLicense;
    private StateLayout mStateLayout;
    private int businessUnitId;
    private int flag = 0;
    private LinearLayout mLlCardView;
    private String mTitle;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_business_unit_info, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    private void initView() {
        mImgLogo = $(R.id.img_company_logo);
        mTvName = $(R.id.tv_company_name);
        mTvPhone = $(R.id.tv_company_phone);
        mTvAddress = $(R.id.tv_company_address);
        mTvDetialaddress = $(R.id.tv_company_detialaddress);
        mImgLicense = $(R.id.img_company_license);
        mStateLayout = $(R.id.statelayout);
        mLlCardView = $(R.id.ll_cardview);
        mStateLayout.showProgressView(false);

        if (getArguments() != null) {
            businessUnitId = getArguments().getInt("businessUnitId");
            flag = getArguments().getInt("flag");
            mTitle = getArguments().getString("title");
        }
        if (flag == 1) {
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
            params.setMargins(ConvertUtils.dip2px(getActivity(), 11), ConvertUtils.dip2px(getActivity(), 5), ConvertUtils.dip2px(getActivity(), 11), 0);
            mLlCardView.setLayoutParams(params);
        }
        if (NetUtils.isHttpConnected(getActivity())) {
            requestData(businessUnitId);
        } else {
            mStateLayout.showNetworkView(false);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mStateLayout.showProgressView(false);
                    requestData(businessUnitId);
                }
            });
        }
    }

    private void requestData(int businessUnitId) {
        Map<String, Object> map = new HashMap<>();
        map.put("businessUnitId", businessUnitId);
        NoHttpUtils.httpPost(UserCenterConstant.GET_BUSINESSUNIT_INFO, map, mOnResponseListener, 0);
    }


    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case 0:
                    if ("0".equals(commonClass.getCode())) {
                        BusinessunitInfo info = JSON.parseObject(commonClass.getData().toString(), BusinessunitInfo.class);
                        if (null != info) {
                            setInfo(info);
                        }
                        mStateLayout.showContentView();
                    } else {
                        mStateLayout.showSystemView(commonClass.getMsg(), false);
                        mStateLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mStateLayout.showProgressView(false);
                                requestData(businessUnitId);
                            }
                        });
                    }
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {
            mStateLayout.showSystemView(result, false);
            mStateLayout.setSystemListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mStateLayout.showProgressView(true);
                    requestData(businessUnitId);
                }
            });
        }

        @Override
        public void onFinish(int what) {
        }
    };

    private void setInfo(BusinessunitInfo info) {
        String businessUnitIcon = info.getBusinessUnitIcon();
        if (!TextUtils.isEmpty(businessUnitIcon)) {
            ImageloaderUtils.displayImg(businessUnitIcon, mImgLogo);
        } else {
            mImgLogo.setImageResource(R.drawable.create_business_logo);
        }
        ImageloaderUtils.displayImg(ImageloaderUtils.getImageLoader(),info.getBusinessLicense(), mImgLicense,ImageloaderUtils.initImageLoaderOptionNoEmptyError(),new ImageloaderUtils.AnimateFirstDisplayListener());
        mTvName.setText(info.getBusinessUnitName());
        String mobilePhone = info.getMobilePhone();
        StringBuilder sb = new StringBuilder();
        if (!TextUtils.isEmpty(mobilePhone)) {
            if (mobilePhone.length() == 11) {
                sb.append(mobilePhone.substring(0, 3)).append("-").append(mobilePhone.substring(3, 7)).append("-").append(mobilePhone.substring(7, 11));
                mTvPhone.setText(sb.toString());
            } else if (mobilePhone.length() == 12) {
                sb.append(mobilePhone.substring(0, 4)).append("-").append(mobilePhone.substring(4, 12));
                mTvPhone.setText(sb.toString());
            } else {
                mTvPhone.setText(mobilePhone);
            }
        }

        mTvAddress.setText(info.getProvince() + info.getCity() + info.getArea());
        mTvDetialaddress.setText(info.getDetailedAddress());
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof BusinessUnitInfoFragment) {
            mActivity.setTitle(mTitle);
        }
    }

    @Override
    public void requestDatas() {
    }
}
